#ifndef BASLERCAMERA_H
#define BASLERCAMERA_H

#include "Logger.h"
#include "commonmethod.h"
#include <QThread>
#include <QObject>
#include <pylon/PylonIncludes.h>
#include <QQuickImageProvider>
#include <QMutex>
#include "silicoolcamera.h"
#include "cameraconfig.h"
SILICOOL_CREATE_LOGGING_CATEGORY(baslerCamera, "BaslerCamera")

using namespace Pylon;
class BaslerCamera :  public SilicoolCamera, public QQuickImageProvider
{
    Q_OBJECT
    enum BaslerCameraControl_Type{
        Type_Basler_Freerun, //设置相机的内触发
        Type_Basler_Line1, //设置相机的外触发
        Type_Basler_ExposureTimeAbs, //设置相机的曝光时间
        Type_Basler_GainRaw, //设置相机的增益
        Type_Basler_AcquisitionFrameRateAbs, //设置相机的频率
        Type_Basler_Width, //图片的宽度
        Type_Basler_Height, //图片的高度
        Type_Basler_LineSource, //灯的触发信号
    };
    class CSampleImageEventHandler : public CImageEventHandler
    {
    private:
        BaslerCamera *pylonCamera;
    public:
        CSampleImageEventHandler(BaslerCamera *pylonCamera) {
            this->pylonCamera = pylonCamera;
        }
        virtual void OnImageGrabbed( CInstantCamera& camera, const CGrabResultPtr& ptrGrabResult)
        {
            pylonCamera->updateImage(ptrGrabResult);
        }
    };
public:
    BaslerCamera();
    ~BaslerCamera() override;
    void updateImage(const CGrabResultPtr& ptrGrabResult);
    QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override;
public:
    SCReturnCode SCCameraInit(QString cameraName) override;
    SCReturnCode SCGetImage(QImage &) override;
    SCReturnCode SCCameraStop() override;
    Q_INVOKABLE bool scSaveImage() override;
private:
    bool init(QString cameraName);
    void setCamera(BaslerCameraControl_Type index, double tmpValue = 0.0); // 设置各种参数
    void copyBufferToQImage(CGrabResultPtr, QImage&);
    QImage getImage();
private:
    CInstantCamera camera;
    CSampleImageEventHandler *imageHandler;
    QMutex mutex;
    QImage latestImage;
protected:
    void run() override;

signals:
    void callQmlRefreshImg();
};

#endif // BASLERCAMERA_H
